package com.caishi.lkx.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.caishi.lkx.user.mapper.UserQuestionRecordsMapper;
import com.caishi.lkx.user.model.UserQuestionRecordsModel;
import com.caishi.lkx.user.service.IUserQuestionRecordsService;
import com.zzw.common.Wrappers;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.exception.BizRuntimeException;

import com.caishi.lkx.user.userlog.model.UserQuestionRecord;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author huangpeng
 * @Description
 * @Date 2022/5/25 10:12
 **/
@Service
public class UserQuestionRecordsServiceImpl implements IUserQuestionRecordsService {

    @Resource
    private UserQuestionRecordsMapper userQuestionRecordsMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public IBaseMapper<UserQuestionRecordsModel, String> getMapper() {
        return userQuestionRecordsMapper;
    }

    @Override
    public void setQuestionRecords(String userId, Long categoryId, String questionTypeName, Integer count, Integer eCount) {
        if (StrUtil.isBlank(userId) || null == categoryId || null == count || null == eCount) {
            throw new BizRuntimeException("参数错误");
        }
        this.setUserQuestionRecord(userId,categoryId,count);
        UserQuestionRecordsModel result = this.selectOne(Wrappers.<UserQuestionRecordsModel>lambdaQuery().eq(UserQuestionRecordsModel::getUserId, userId).eq(UserQuestionRecordsModel::getCategoryId, categoryId));
        if (null == result) {
            UserQuestionRecordsModel model = new UserQuestionRecordsModel();
            model.setUserId(userId);
            model.setCategoryId(categoryId);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("count", count);
            jsonObject.put("eCount", eCount);
            Map<String, JSONObject> map = new HashMap<>();
            map.put(questionTypeName, jsonObject);
            model.setQuestionCount(map);
            this.insert(model);
            return;
        }
        Map<String, JSONObject> questionCountMap = result.getQuestionCount();
        JSONObject jsonObject = questionCountMap.get(questionTypeName);
        JSONObject questionNew = new JSONObject();
        if (null != jsonObject) {
            Long oldCount = jsonObject.getLong("count");
            Long oldECount = jsonObject.getLong("eCount");
            questionNew.put("count", oldCount + count);
            questionNew.put("eCount", oldECount + eCount);
        } else {
            questionNew.put("count", count);
            questionNew.put("eCount", eCount);
        }
        questionCountMap.put(questionTypeName, questionNew);
        result.setQuestionCount(questionCountMap);
        this.update(result);
    }

    private void setUserQuestionRecord(String userId, Long categoryId, Integer count) {
        Query query = new Query(Criteria.where("userId").is(userId).and("categoryId").is(categoryId).and("date").is(LocalDate.now()));
        UserQuestionRecord userQuestionRecords = mongoTemplate.findOne(query, UserQuestionRecord.class);
        if (null != userQuestionRecords) {
            userQuestionRecords.setCount(userQuestionRecords.getCount() + (count != null ? count : 0));
            mongoTemplate.save(userQuestionRecords);
        } else {
            UserQuestionRecord userQuestionRecord = new UserQuestionRecord();
            userQuestionRecord.setDate(LocalDate.now());
            userQuestionRecord.setUserId(userId);
            userQuestionRecord.setCount(count);
            userQuestionRecord.setCategoryId(categoryId);
            mongoTemplate.save(userQuestionRecord);
        }
    }
}
